﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;

namespace Rcon
{
	/// <summary>
	/// Known map names:
	/// Array;mp_array
	/// Havana;mp_cairo
	/// Launch;mp_cosmodrome
	/// Cracked;mp_cracked
	/// Crisis;mp_crisis
	/// Grid;mp_duga
	/// Firing Range;mp_firingrange
	/// Hanoi;mp_hanoi
	/// Jungle;mp_havoc
	/// Summit;mp_mountain
	/// Nuke Town;mp_nuked
	/// Radiation;mp_radiation
	/// WMD;mp_russianbase
	/// Villa;mp_villa
	/// </summary>
	public class MapLookup
	{
		private Dictionary<string, string> pretty2Ugly = new Dictionary<string, string>();
		private Dictionary<string, string> ugly2Pretty = new Dictionary<string, string>();

		public MapLookup()
		{
			FileInfo mapFile = new FileInfo("Configs\\mapList.cfg");
            if(mapFile.Exists)                  // Found the Map List Config file, load from there
            {
                string[] configStrings = File.ReadAllLines(mapFile.FullName);
                string[] args;
                foreach(string entry in configStrings)
                {
                    if(entry.StartsWith("//"))
                        continue;
                    args = Regex.Split(entry, ";");
                    pretty2Ugly.Add(args[0], args[1]);
                }
            }
            else                                // Can't find Map List Config file, use map defaults (Maps as of 9 November 2010)
            {
                pretty2Ugly.Add("Array",		"mp_array");
			    pretty2Ugly.Add("Havana",		"mp_cairo");
			    pretty2Ugly.Add("Launch",		"mp_cosmodrome");
			    pretty2Ugly.Add("Cracked",		"mp_cracked");
			    pretty2Ugly.Add("Crisis",		"mp_crisis");
			    pretty2Ugly.Add("Grid",			"mp_duga");
			    pretty2Ugly.Add("Firing Range", "mp_firingrange");
			    pretty2Ugly.Add("Hanoi",		"mp_hanoi");
			    pretty2Ugly.Add("Jungle",		"mp_havoc");
			    pretty2Ugly.Add("Summit",		"mp_mountain");
			    pretty2Ugly.Add("Nuke Town",	"mp_nuked");
			    pretty2Ugly.Add("Radiation",	"mp_radiation");
			    pretty2Ugly.Add("WMD",			"mp_russianbase");
			    pretty2Ugly.Add("Villa",		"mp_villa");
            }
			foreach (string key in pretty2Ugly.Keys)
			{
				ugly2Pretty.Add(pretty2Ugly[key], key);
			}
		}

		/// <summary>
		/// NOT IMPLEMENTED, haven't decided on a file format
		/// </summary>
		/// <param name="mapFile"></param>
		public MapLookup(string mapFile)
		{
			// not implemented
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="uglyName"></param>
		/// <returns>NULL if uglyName is not known</returns>
		public string GetPrettyName(string uglyName)
		{
			if (ugly2Pretty.ContainsKey(uglyName))
			{
				return ugly2Pretty[uglyName];
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="prettyName"></param>
		/// <returns>NULL if prettyName is not known</returns>
		public string GetUglyName(string prettyName)
		{
			if (pretty2Ugly.ContainsKey(prettyName))
			{
                return pretty2Ugly[prettyName];
			}
			else
			{
				return null;
			}
		}

        public bool IsValidName(string name)
        {
            if (name.Equals("Any"))
                return true;
            return (pretty2Ugly.ContainsKey(name) || ugly2Pretty.ContainsKey(name));
        }

        public string[] GetPrettyNames() 
        { 
            string[] sa = new string[pretty2Ugly.Keys.Count]; 
            pretty2Ugly.Keys.CopyTo(sa, 0); 
            return sa; 
        }    
        
        public string[] GetUglyNames() 
        { 
            string[] sa = new string[ugly2Pretty.Keys.Count]; 
            ugly2Pretty.Keys.CopyTo(sa, 0); 
            return sa; 
        }  
	}
}
